package com.yeqian.user.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BiliBiliUtil {

    /**
     * 获取总播放数、阅读量
     */

    public static Map<String,Object> getReadingsTotalPlays(String mid){
        Map<String,Object> map=new HashMap<>();
        String getResult = HttpUtil
                .createGet("https://api.bilibili.com/x/space/upstat?mid="+mid)
                .execute()
                .charset("gbk")
                .body();
        String data = JSONObject.parseObject(getResult).get("data").toString();
        JSONObject jsonObject = JSONObject.parseObject(data);

        String archive = jsonObject.get("archive").toString();
        JSONObject archiveJsonObject = JSONObject.parseObject(archive);
        map.put("总播放数",archiveJsonObject.get("view"));

        String article = jsonObject.get("article").toString();
        JSONObject articleJsonObject = JSONObject.parseObject(article);
        map.put("阅读量",articleJsonObject.get("view").toString());
        return map;
    }

    /**
     * 获取粉丝、关注数量
     */

    public static Map<String,Object> GetFansFttention(String mid){
        Map<String,Object> map=new HashMap<>();
        String getResult = HttpUtil
                .createGet("https://api.bilibili.com/x/relation/stat?vmid="+mid)
                .execute()
                .charset("gbk")
                .body();
        String data = JSONObject.parseObject(getResult).get("data").toString();
        JSONObject jsonObject = JSONObject.parseObject(data);
        map.put("关注",jsonObject.get("following").toString());
        map.put("粉丝",jsonObject.get("follower").toString());
        return map;
    }

    /**
     *
     * 获取UP主信息（名称、性别、头像、描述、个人认证信息、大会员状态、直播间地址、预览图、标题、房间号、观看人数、直播间状态[开启/关闭]等） 作者：可爱的小喵咪Cat https://www.bilibili.com/read/cv12357091/ 出处：bilibili
     * @param mid
     * @return
     */
    public static Map<String,Object> GetUPMasterInformation(String mid){
        Map<String,Object> map=new HashMap<>();
        String getResult = HttpUtil
                .createGet("https://api.bilibili.com/x/space/acc/info?mid="+mid)
                .execute()
                .charset("utf8")
                .body();
        String data = JSONObject.parseObject(getResult).get("data").toString();
        JSONObject jsonObject = JSONObject.parseObject(data);
        map.put("UP名称",jsonObject.get("name").toString());
        map.put("性别",jsonObject.get("sex").toString());
        map.put("头像",jsonObject.get("face").toString());
        map.put("个性签名",jsonObject.get("sign").toString());
        map.put("B站等级",jsonObject.get("level").toString());
        return map;
    }
}
